package com.cloudkinto.controller.product;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.product.ProductCategoryAddBo;
import com.cloudkinto.bo.product.ProductCategoryExportBo;
import com.cloudkinto.bo.product.ProductCategoryQueryBo;
import com.cloudkinto.bo.product.ProductCategoryUpdateBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.product.ProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author long
 * @since 2024-10-29
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "omp产品分类")
@RequestMapping("/api/admin/omp/productCategory")
public class ProductCategoryOmpController extends BaseController {

    private final ProductCategoryService productCategoryService;

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "产品分类新增", notes = "产品分类新增")
    public SingleResult add(@RequestBody @Validated ProductCategoryAddBo bo) {
        productCategoryService.add(bo);
        return SingleResult.success("新增成功");
    }

    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "产品分类修改", notes = "产品分类修改")
    public SingleResult update(@RequestBody @Validated ProductCategoryUpdateBo bo) {
        productCategoryService.update(bo);
        return SingleResult.success("修改成功");
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "产品分类根据id删除", notes = "产品分类根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        return SingleResult.success("删除成功", productCategoryService.delete(id));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "产品分类查询（分页）", notes = "产品分类查询（分页）")
    public PageResult listPage(ProductCategoryQueryBo bo) {
        bo.setTenantId(getTenantId());
        return productCategoryService.getListPage(bo);
    }

    @UserSessionRequired
    @GetMapping("/getCategoryTree")
    @ApiOperation(value = "产品分类树查询", notes = "产品分类树查询")
    public SingleResult getCategoryTree() {
        return SingleResult.success("查询成功", productCategoryService.getCategoryTree(getTenantId()));
    }

    @UserSessionRequired
    @PostMapping("/importCategory")
    @ApiOperation(value = "产品分类导入", notes = "产品分类导入")
    public SingleResult importCategory(MultipartFile file) {
        productCategoryService.importCategory(file, getTenantId());
        return SingleResult.success("导入成功");
    }

    @UserSessionRequired
    @PostMapping("/exportCategory")
    @ApiOperation(value = "产品分类导出", notes = "产品分类导出")
    public void exportCategory(@RequestBody ProductCategoryExportBo bo, HttpServletResponse response) throws IOException {
        productCategoryService.exportCategory(
                bo.getIds(),
                getTenantId(),
                response
        );
    }

}

